package com.yanqu.road.dao.impl.activity.patronsfund;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.patronsfund.PatronsFundBuyData;
import com.yanqu.road.entity.activity.patronsfund.PatronsFundUser;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class PatronsFundUserDaoImpl extends TempDataObjectDao<PatronsFundUser> {

    public static String selectSql = "select * from t_u_patrons_fund_user where userId = ? and activityId = ?";
    public Map<Integer, PatronsFundUser> getPatronsFundUser(long userId, int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, PatronsFundUser> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(selectSql);
            int i = 1;
            call.setLong(i++, userId);
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                PatronsFundUser data = parse(rs);
                map.put(data.getPatronsId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String insertSql = "INSERT INTO `t_u_patrons_fund_user`(`userId`, `patronsId`, `activityId`, `patronsQua`, `patronsRela`, `createTime`, `updateTime`) VALUES (?, ?, ?, ?, ?, ?, ?)";
    @Override
    protected boolean addData(PatronsFundUser data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPatronsId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getPatronsQua()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getPatronsRela()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getCreateTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUpdateTime()));
        if (getDb().executeNoneQuery(insertSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateSql = "UPDATE `t_u_patrons_fund_user` SET `patronsQua` = ?, `patronsRela` = ?, `updateTime` = ? WHERE `userId` = ? AND `patronsId` = ? AND `activityId` = ? ";
    @Override
    protected boolean updateData(PatronsFundUser data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getPatronsQua()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getPatronsRela()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUpdateTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPatronsId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        if (getDb().executeNoneQuery(updateSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected PatronsFundUser parseData(ResultSet rs) throws SQLException {
        PatronsFundUser patronsFundUser = new PatronsFundUser();
        patronsFundUser.setUserId(rs.getLong("userId"));
        patronsFundUser.setPatronsId(rs.getInt("patronsId"));
        patronsFundUser.setActivityId(rs.getInt("activityId"));
        patronsFundUser.setPatronsQua(rs.getLong("patronsQua"));
        patronsFundUser.setPatronsRela(rs.getLong("patronsRela"));
        patronsFundUser.setCreateTime(rs.getLong("createTime"));
        patronsFundUser.setUpdateTime(rs.getLong("updateTime"));
        return patronsFundUser;
    }
}
